package view.player;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import constants.Icons;
import controller.CompleteListener;


import view.BaseComponent;


public class PlayerPanel extends BaseComponent implements ActionListener, FocusListener {

	private int playerIconIndex; // TODO UNUSED IN THE CURRENT VERSION
	
	private int id;
	
	private JLabel playerIcon;
	private JButton fullStuff;
	private JButton noStuff;
	private GrenadeCountPanel hePan;
	private GrenadeCountPanel fbPan;
	private GrenadeCountPanel sgPan;
	private JTextField nameField;
	
	public PlayerPanel(int id_, String name) {
		
		JPanel row1 = new JPanel();
		JPanel col0 = new JPanel();
		JPanel col1 = new JPanel();
		JPanel fakeRow = new JPanel();
		
		id = id_;
		
		playerIcon = new JLabel(Icons.getIcon("rifle"));
		fullStuff = new JButton(Icons.getIcon("fullStuff"));
		noStuff = new JButton(Icons.getIcon("noStuff"));
		hePan = new GrenadeCountPanel("HE");
		fbPan = new GrenadeCountPanel("FB");
		sgPan = new GrenadeCountPanel("SG");
		nameField = new JTextField(name,10);
		
		fullStuff.setActionCommand("fullStuff");
		fullStuff.setToolTipText("Equips the player with all the possible stuff");
		noStuff.setActionCommand("noStuff");
		noStuff.setToolTipText("Strips the player from all its stuff");
		
		nameField.addFocusListener(this);
		
		fullStuff.addActionListener(this);
		noStuff.addActionListener(this);
		
		fullStuff.setMaximumSize(new Dimension(fullStuff.getIcon().getIconWidth(),
				fullStuff.getIcon().getIconHeight()));
		fullStuff.setPreferredSize(new Dimension(fullStuff.getIcon().getIconWidth(),
				fullStuff.getIcon().getIconHeight()));
		noStuff.setMaximumSize(new Dimension(noStuff.getIcon().getIconWidth(),
				noStuff.getIcon().getIconHeight()));
		noStuff.setPreferredSize(new Dimension(noStuff.getIcon().getIconWidth(),
				noStuff.getIcon().getIconHeight()));

		row1.setLayout(new BoxLayout(row1,BoxLayout.LINE_AXIS));
		col0.setLayout(new BoxLayout(col0,BoxLayout.PAGE_AXIS));
		col1.setLayout(new BoxLayout(col1,BoxLayout.PAGE_AXIS));
		fakeRow.setLayout(new BoxLayout(fakeRow,BoxLayout.LINE_AXIS));
		this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		
		playerIcon.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
		nameField.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLoweredBevelBorder(),
				BorderFactory.createEmptyBorder(3,3,3,3)));
		this.setBorder(BorderFactory.createLoweredBevelBorder());
		
		nameField.setMaximumSize(new Dimension(100,25));
		nameField.setPreferredSize(new Dimension(100,25));
		
		col1.setMaximumSize(new Dimension(100,9999));
		
		col1.add(hePan);
		col1.add(fbPan);
		col1.add(sgPan);
		

		row1.add(playerIcon);
		row1.add(col1);
		row1.add(Box.createHorizontalGlue());
		
		fakeRow.add(nameField);
		fakeRow.add(Box.createHorizontalStrut(2));
		fakeRow.add(fullStuff);
		fakeRow.add(noStuff);
		
		fakeRow.setMaximumSize(new Dimension(999999, 25));
		
		this.add(row1);
		this.add(fakeRow);
		
//		this.setMaximumSize(new Dimension(200,110));
//		this.setPreferredSize(new Dimension(200,110));
	}
	
	public void setSelected(boolean selected) {
		if (selected)
			this.setBorder(BorderFactory.createLineBorder(Color.red, 2));
		else
			this.setBorder(BorderFactory.createLoweredBevelBorder());
	}
	
	// TODO UNUSED IN THE CURRENT VERSION
	public void incIconIndex() {
		
	}
	
	// TODO UNUSED IN THE CURRENT VERSION
	public void decIconIndex() {
		
	}
	
	public String getPlayersName() {
		return nameField.getText();
	}
	
	public void setPlayersName(String name) {
		nameField.setText(name);
	}
	
	@Override
	public void attachListener(CompleteListener l) {
		hePan.attachListener(l);
		fbPan.attachListener(l);
		sgPan.attachListener(l);
		
		fullStuff.addActionListener(l);
		noStuff.addActionListener(l);
		
		nameField.addFocusListener(l);
	}

	@Override
	public void setEnabled(boolean enable) {
		hePan.setEnabled(enable);
		fbPan.setEnabled(enable);
		sgPan.setEnabled(enable);
		
		fullStuff.setEnabled(enable);
		noStuff.setEnabled(enable);
	}
	
	public void setNameFieldEnabled(boolean enable) {
		nameField.setEnabled(enable);
	}

	public int getId() {
		return id;
	}

	public void actionPerformed(ActionEvent e) {}

	// This method is called only when the name field is
	// getting the focus :
	public void focusGained(FocusEvent e) {
		nameField.selectAll();
	}

	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public JTextField getNamefield() {
		return nameField;
	}
	
	public GrenadeCountPanel getFbPan() {
		return fbPan;
	}

	public GrenadeCountPanel getHePan() {
		return hePan;
	}

	public GrenadeCountPanel getSgPan() {
		return sgPan;
	}
}
